package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.entity.UserRole;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.SysRoleMapper;
import com.zzyl.mapper.SysRoleResourceMapper;
import com.zzyl.mapper.SysUserRoleMapper;
import com.zzyl.service.SysRoleService;
import com.zzyl.utils.EmptyUtil;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.RoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysRoleResourceMapper sysRoleResourceMapper;

    @Override
    public void createRole(RoleDto roleDto) {
        Integer count = sysRoleMapper.selectCountByRoleName(roleDto.getRoleName());
        if (count > 0) {
            throw new BaseException(BasicEnum.ROLE_NAME_DUPLICATE_EXCEPTION);
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleDto, role);
        sysRoleMapper.createRole(role);
    }

    @Override
    public PageResponse<RoleVo> getRoleByPage(String roleName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Role> roles = sysRoleMapper.selectByPage(roleName);
        PageResponse<RoleVo> roleVoPageResponse = PageResponse.of(roles, RoleVo.class);
        return roleVoPageResponse;
    }

    @Override
    public void updateRole(RoleDto roleDto) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDto, role);
        //判断是否修改角色对应的资源数据
        if (ObjectUtil.isEmpty(roleDto.getCheckedResourceNos())) {
            Integer count = sysUserRoleMapper.countByRoleId(role.getId());
            if (count > 0 && role.getDataState().equals("1")) {
                throw new BaseException(BasicEnum.ROLE_MODIFIED_FAIL);
            }
            sysRoleMapper.updateByPrimaryKeySelective(role);
            return;
        }

        //删除原有角色资源中间信息
        sysRoleResourceMapper.deleteRoleResourceByRoleId(role.getId());
        //保存角色资源中间信息
        List<RoleResource> roleResourceList = Lists.newArrayList();
        Arrays.asList(roleDto.getCheckedResourceNos()).forEach(n -> {
            RoleResource roleResource = RoleResource.builder()
                    .roleId(role.getId())
                    .resourceNo(n)
                    .dataState(SuperConstant.DATA_STATE_0)
                    .build();
            roleResourceList.add(roleResource);
        });
        //如果集合为空，则结束请求
        if (EmptyUtil.isNullOrEmpty(roleResourceList)) {
            return ;
        }
        //批量保存角色和资源的关系数据
        sysRoleResourceMapper.batchInsert(roleResourceList);
    }

    @Override
    public void deleteRole(Long[] roleIds) {
        List<UserRole> userRoles = sysUserRoleMapper.selectByRoleId(roleIds);
        if (userRoles.size() > 0) {
            throw new BaseException(BasicEnum.ROLE_DISTRIBUTED);
        }
        for (Long roleId : roleIds) {
            sysRoleMapper.deleteByPrimaryKey(roleId);
        }
    }

    @Override
    public List<String> getResourcesbyRoleId(Long roleId) {
        return sysUserRoleMapper.getResourcesbyRoleId(roleId);
    }

    @Override
    public List<RoleVo> initRoles() {
        return sysRoleMapper.initRoles();
    }
}
